package com.ravi.controller;

import com.ravi.common.R;
import com.ravi.core.checkup.CheckingUp;
import com.ravi.core.checkup.CheckingUpManager;
import com.ravi.core.patient.BasePatient;
import com.ravi.core.queue.CheckUpQueue;
import com.ravi.core.queue.HashQueueManager;
import com.ravi.entity.PatientQueueInfo;
import com.ravi.service.PatientQueueInfoService;
import com.ravi.util.jwt.JWTUtils;
import com.ravi.vo.CheckingPatientVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@CrossOrigin
@Slf4j
@RequestMapping("checkingup")
public class CheckingUpController {

	@Autowired
	private PatientQueueInfoService patientQueueInfoService;

	@GetMapping("calling")
	public R callingPatient(HttpServletRequest request) {
		final Integer roomId = JWTUtils.getRoomId(request.getHeader("token"));
		CheckingUp checkingUp = CheckingUpManager.getRoom(roomId);
		CheckUpQueue checkUpQueue = HashQueueManager.getQueue(roomId);
		if (checkUpQueue.size() > 0) {
			BasePatient basePatient = checkUpQueue.poll();
			PatientQueueInfo patientInfo = patientQueueInfoService.getPatientInfoById(basePatient.getId());
			checkingUp.setPatientCalling(new CheckingPatientVo(patientInfo.getPatientId(), patientInfo.getName()));
		}
		return R.ok("叫号成功");
	}

	@GetMapping("getCallingMsg")
	public R getCallingMsg(HttpServletRequest request) {
		final Integer roomId = JWTUtils.getRoomId(request.getHeader("token"));
		CheckingUp checkingUp = CheckingUpManager.getRoom(roomId);
		if (checkingUp.getPatientCalling() != null) {
			return R.ok().put("callingMsg", checkingUp.getPatientCalling().toString());
		}else {
			return R.ok().put("callingMsg", "暂无");
		}
	}
}
